import 'package:lishopflu/base/controller_import.dart';

import 'home_ctl.dart';
import 'home_page.dart';
import 'message_ctl.dart';
import 'message_page.dart';
import 'my_ctl.dart';
import 'my_page.dart';
import 'shop_car_ctl.dart';
import 'shop_car_page.dart';
import 'story_ctl.dart';
import 'story_page.dart';

class MainCtl extends BaseController{

  RxInt page = 0.obs;
  final pages = <String>[AppPages.HOME, AppPages.STORY, AppPages.MESSAGE, AppPages.SHOP_CART, AppPages.MY];
  PageController pageController;


  @override
  void onFirst() {
    pageController = PageController(initialPage: page.value);
  }

  Route onGenerateRoute(RouteSettings settings) {
    switch(settings.name){
      case AppPages.HOME:
        return GetPageRoute(
          settings: settings,
          page: () => HomePage(),
          binding: BindingsBuilder.put (() => HomeCtl()),
        );
      case AppPages.STORY:
        return GetPageRoute(
          settings: settings,
          page: () => StoryPage(),
          binding: BindingsBuilder.put (() => StoryCtl()),
        );
      case AppPages.MESSAGE:
        return GetPageRoute(
          settings: settings,
          page: () => MessagePage(),
          binding: BindingsBuilder.put (() => MessageCtl()),
        );
      case AppPages.SHOP_CART:
        return GetPageRoute(
          settings: settings,
          page: () => ShopCarPage(),
          binding: BindingsBuilder.put (() => ShopCarCtl()),
        );
      case AppPages.MY:
        return GetPageRoute(
          settings: settings,
          page: () => MyPage(),
          binding: BindingsBuilder.put (() => MyCtl()),
        );
    }
    return null;
  }

  @override
  void onDestroy() {
    pageController.dispose();
  }

  void onTap(int index) => pageController.animateToPage(index, duration: const Duration(milliseconds: 300), curve: Curves.ease);

  void changePage(int index) {
    page.value = index;
    Get.toNamed(pages[index], id: 1);
  }

  void onPageChanged(int page) => this.page.value = page;
}